
#include "option.h"
#include "BaseStruct.h"
#include "IOCPServer.h"
#include "Player.h"
#include "PacketFactory.h"

Player::Player()
{

}

Player::~Player()
{

}

void Player::Init()
{

}


bool Player::AddItem(uint32 index,psu_item* pItem)
{
	if(Inventory.find(index)==Inventory.end())
	{
		Inventory.insert(INVENTORY_MAP_PAIR(index,pItem));
		return true;
	}
	else
	{
        return false;
	}
}

psu_item* Player::GetItem(uint32 index)
{
	if(Inventory.find(index)!=Inventory.end())
	{
        return Inventory[index]; 
	}
	else
	{
		return NULL;
	}
}

void Player::EquipItem(ItemOperateParam &ItemParam)
{
    psu_item* pItem = GetItem(ItemParam.ItemIndex);
	if(pItem==NULL) return;
	uint8 Category = *(uint8*)&(pItem->Itemid);
	if(pItem->type==clothing_item)
	{
        
	}
	else if (pItem->type==consumable_item)
	{

	}
	else if (pItem->type==parts_item)
	{

	}
	else if (pItem->type==special_item)
	{    
      
		Packet* pSendMsg = PacketFactory::Instance()->Alloc();
        *pSendMsg<<(uint32)0x01050300;
		*pSendMsg<<(uint64)0;
		*pSendMsg<<ItemParam.TargetGID;
		*pSendMsg<<(uint64)0;
		*pSendMsg<<(uint32)0x00011300;
        *pSendMsg<<ItemParam.GID;
		*pSendMsg<<(uint64)0;
		*pSendMsg<<ItemParam.TargetGID;
		*pSendMsg<<ItemParam.TargetLID;
		*pSendMsg<<ItemParam.ItemIndex;
		*pSendMsg<<(uint8)1;
		*pSendMsg<<Category;
		*pSendMsg<<ItemParam.varH;
		*pSendMsg<<ItemParam.varI;
		IOCPServer::Instance()->Send(ItemParam.lpSocketFD,pSendMsg->getBuff(),pSendMsg->getSize());
		PacketFactory::Instance()->Free(pSendMsg);

	}
	else if(pItem->type==striking_weapon_item)
	{
		uint32 SoundInt = 0;
        psu_striking_weapon_item* pWeaponItem = (psu_striking_weapon_item*)pItem; 

		Packet* pSendMsg = PacketFactory::Instance()->Alloc();
		*pSendMsg<<(uint32)0x01050300;
		*pSendMsg<<(uint64)0;
		*pSendMsg<<ItemParam.TargetGID;
		*pSendMsg<<(uint64)0;
		*pSendMsg<<(uint32)0x00011300;
		*pSendMsg<<ItemParam.GID;
		*pSendMsg<<(uint64)0;
		*pSendMsg<<ItemParam.TargetGID;
		*pSendMsg<<ItemParam.TargetLID;
		*pSendMsg<<ItemParam.ItemIndex;
		*pSendMsg<<(uint8)1;
		*pSendMsg<<Category;
		*pSendMsg<<ItemParam.varH;
		*pSendMsg<<ItemParam.varI;
		*pSendMsg<<SoundInt;
		*pSendMsg<<pWeaponItem->hitbox_a;
		*pSendMsg<<pWeaponItem->hitbox_b;
		*pSendMsg<<pWeaponItem->hitbox_c;
		*pSendMsg<<pWeaponItem->hitbox_d;
		*pSendMsg<<pWeaponItem->bysound_nb;
		*pSendMsg<<(uint8)0;
		*pSendMsg<<pWeaponItem->effect;
		*pSendMsg<<pWeaponItem->model;

		IOCPServer::Instance()->Send(ItemParam.lpSocketFD,pSendMsg->getBuff(),pSendMsg->getSize());
		PacketFactory::Instance()->Free(pSendMsg);

	}
	else if (pItem->type==trap_item)
	{    
	
		 Binary BinData;
         psu_trap_item* pTrapItem = (psu_trap_item*)pItem; 
		 uint8 var[4] = {0,0,0,0};
		 var[0] = pTrapItem->effect;
		 uint32 param = 0;
		 switch (pTrapItem->traptype)
		 {
			

		 case damage:
			 param = *(uint32*)var|0x0c0a05;
			 BinData<<pTrapItem->effect;
			 BinData<<param;
			 BinData<<(uint32)0x20140500;
			 BinData<<(uint32)0x0001c800;
			 BinData<<(uint32)0x10000000;
			 break;
		 case damage_g:
			 param = *(uint32*)var|0x2c0505;
			 BinData<<pTrapItem->effect;
			 BinData<<param;
			 BinData<<(uint32)0x0c000600;
			 BinData<<(uint32)0x00049001;
			 BinData<<(uint32)0x10000000;
			 break;
		 case trap:
			 param = *(uint32*)var|0x0d0a05;
			 BinData<<pTrapItem->effect;
			 BinData<<param;
			 BinData<<(uint32)0x61140000;
			 BinData<<(uint32)0x0001c800;
			 BinData<<(uint32)0x10000000;
			 break;
		 case trap_g:
			 param = *(uint32*)var|0x4d0505;
			 BinData<<pTrapItem->effect;
			 BinData<<param;
			 BinData<<(uint32)0x4d000000;
			 BinData<<(uint32)0x00049001;
			 BinData<<(uint32)0x10000000;
			 break;
		 case trap_ex:
			 param = *(uint32*)var|0x490a05;
			 BinData<<pTrapItem->effect;
			 BinData<<param;
			 BinData<<(uint32)0x4500000f;
			 BinData<<(uint32)0x4b055802;
			 BinData<<(uint32)0x10000000;
			 break;

		 }
		
		 Packet* pSendMsg = PacketFactory::Instance()->Alloc();
		 *pSendMsg<<(uint32)0x01050300;
		 *pSendMsg<<(uint64)0;
		 *pSendMsg<<ItemParam.TargetGID;
		 *pSendMsg<<(uint64)0;
		 *pSendMsg<<(uint32)0x00011300;
		 *pSendMsg<<ItemParam.GID;
		 *pSendMsg<<(uint64)0;
		 *pSendMsg<<ItemParam.TargetGID;
		 *pSendMsg<<ItemParam.TargetLID;
		 *pSendMsg<<ItemParam.ItemIndex;
		 *pSendMsg<<(uint8)1;
		 *pSendMsg<<Category;
		 *pSendMsg<<ItemParam.varH;
		 *pSendMsg<<ItemParam.varI;
		 *pSendMsg<<BinData;

		 IOCPServer::Instance()->Send(ItemParam.lpSocketFD,pSendMsg->getBuff(),pSendMsg->getSize());
		 PacketFactory::Instance()->Free(pSendMsg);


	}

}

void Player::UnEquipItem(ItemOperateParam &ItemParam)
{
	psu_item* pItem = GetItem(ItemParam.ItemIndex);
	if(pItem==NULL) return;
	uint8 Category = 0;
    switch (ItemParam.ItemIndex)
    {
	case 19:
		Category = 2;
		break;
	default:
		Category = 1;
		break;
    }

	Packet* pSendMsg = PacketFactory::Instance()->Alloc();
	*pSendMsg<<(uint32)0x01050300;
	*pSendMsg<<(uint64)0;
	*pSendMsg<<ItemParam.GID;
	*pSendMsg<<(uint64)0;
	*pSendMsg<<(uint32)0x00011300;
	*pSendMsg<<ItemParam.GID;
	*pSendMsg<<(uint64)0;
	*pSendMsg<<ItemParam.TargetGID;
	*pSendMsg<<ItemParam.TargetLID;
	*pSendMsg<<ItemParam.ItemIndex;
	*pSendMsg<<(uint8)2;
	*pSendMsg<<Category;
	*pSendMsg<<ItemParam.varH;
	*pSendMsg<<ItemParam.varI;
	IOCPServer::Instance()->Send(ItemParam.lpSocketFD,pSendMsg->getBuff(),pSendMsg->getSize());
	PacketFactory::Instance()->Free(pSendMsg);
	

}

void Player::SetTrap(ItemOperateParam &ItemParam)
{    
	psu_item* pItem = GetItem(ItemParam.ItemIndex);
	if(pItem==NULL) return;
    uint8 Category = *(uint8*)&(pItem->Itemid);
	
	Packet* pSendMsg = PacketFactory::Instance()->Alloc();
	*pSendMsg<<(uint32)0x01050300;
	*pSendMsg<<(uint64)0;
	*pSendMsg<<ItemParam.GID;
	*pSendMsg<<(uint64)0;
	*pSendMsg<<(uint32)0x00011300;
	*pSendMsg<<ItemParam.GID;
	*pSendMsg<<(uint64)0;
	*pSendMsg<<ItemParam.TargetGID;
	*pSendMsg<<ItemParam.TargetLID;
	*pSendMsg<<ItemParam.ItemIndex;
	*pSendMsg<<(uint8)9;//item type
	*pSendMsg<<Category;
	*pSendMsg<<ItemParam.varH;
	*pSendMsg<<ItemParam.varI;
	IOCPServer::Instance()->Send(ItemParam.lpSocketFD,pSendMsg->getBuff(),pSendMsg->getSize());
	PacketFactory::Instance()->Free(pSendMsg);
}